package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Models.Product;
import Models.Provider;

public class ProviderServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action = req.getParameter("action") != null? req.getParameter("action") : "";
		if(action.equals("new")){
			getServletContext().getRequestDispatcher("/jsp/provider/new.jsp").forward(req, resp);
		}else if(action.equals("show")){//show
			String id = req.getParameter("id");
			try {
				req.setAttribute("provider", Provider.LoadSql(Integer.parseInt(id)));
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			getServletContext().getRequestDispatcher("/jsp/provider/show.jsp").forward(req, resp);
		
		}else if(action.equals("edit")){//edit
			String id = req.getParameter("id");
			try {
				req.setAttribute("provider", Provider.LoadSql(Integer.parseInt(id)));
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			getServletContext().getRequestDispatcher("/jsp/provider/edit.jsp").forward(req, resp);
		
		}else if(action.equals("delete")){//delete
			String id = req.getParameter("id");
			try {
				Provider provider = Provider.LoadSql(Integer.parseInt(id));
				Provider.delete(provider);
			} catch (NumberFormatException e) {e.printStackTrace();}
			
			req.setAttribute("list_providers", Provider.LoadSql());
			getServletContext().getRequestDispatcher("/jsp/provider/index.jsp").forward(req, resp);
		}else{//index
			req.setAttribute("list_providers", Provider.LoadSql());
			getServletContext().getRequestDispatcher("/jsp/provider/index.jsp").forward(req, resp);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		String action = req.getParameter("action") != null? req.getParameter("action") : "";
		if(action.equals("new")){
			System.out.println("create");
			
			try {
				Provider provider = new Provider();
				provider.setName(req.getParameter("first_name"));
				if(provider.getName().equals("")) req.setAttribute("msg", "Champs manquant(s)");
				provider.setSurname(req.getParameter("last_name"));
				if(provider.getSurname().equals("")) req.setAttribute("msg", "Champs manquant(s)");
				provider.setPhoneNumber(req.getParameter("phone"));
				if(provider.getPhoneNumber().equals("")) req.setAttribute("msg", "Champs manquant(s)");
				provider.setAddress(req.getParameter("adress"));
				if(provider.getAddress().equals("")) req.setAttribute("msg", "Champs manquant(s)");
				provider.setEmailAdress(req.getParameter("email"));
				if(provider.getEmailAdress().equals("")) req.setAttribute("msg", "Champs manquant(s)");
				provider.setCity(req.getParameter("town"));
				if(provider.getCity().equals("")) req.setAttribute("msg", "Champs manquant(s)");
				provider.setCountry(req.getParameter("country"));
				if(provider.getCountry().equals("")) req.setAttribute("msg", "Champs manquant(s)");
				if(req.getAttribute("msg") != null){
					doGet(req, resp);
					return;
				}else if(Provider.Save(provider)){
					resp.sendRedirect("/SiteERP/provider/");
					return;
				}
			} catch (NumberFormatException e) {e.printStackTrace();}
			req.setAttribute("msg", "Echec cr&eacute;ation fournisseur");
			doGet(req, resp);
		}else if(action.equals("edit")){
			int id = Integer.parseInt(req.getParameter("id"));
			Provider provider = Provider.LoadSql(id);
			provider.setName(req.getParameter("first_name"));
			provider.setSurname(req.getParameter("last_name"));
			provider.setPhoneNumber(req.getParameter("phone"));
			provider.setAddress(req.getParameter("adress"));
			provider.setCity(req.getParameter("town"));
			provider.setCountry(req.getParameter("country"));
			
			if(provider.update()){
				resp.sendRedirect("/SiteERP/provider/");
				return;
			}else
				req.setAttribute("msg", "Echec cr&eacute;ation fournisseur");
			
			doGet(req, resp);
			
		}else{//index
			doGet(req, resp);
		}
		
	}
	
}
